package com.artwork.controller;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.Ticket;
import com.artwork.model.User;
import com.artwork.service.TicketService;
import com.artwork.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author JoJo
 * @Data 2021/9/9$ 15:15$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/admin")
public class TicketController {
    @Autowired
    TicketService ticketService;
    @Autowired
    UserService userService;

    /**
     * @desc 新增代金券
     * @param ticket
     * @return
     * @throws JsonException
     */
    @PostMapping("/ticket/add")
    public AjaxResult addTicket(Ticket ticket) throws JsonException{
        User user = userService.getUserById(ticket.getUserId());
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        ticketService.addTicket(ticket);
        return AjaxResult.success();
    }

    /**
     * @desc 修改代价券
     * @param ticket
     * @return
     * @throws JsonException
     */
    @PostMapping("/ticket/update")
    public AjaxResult updateTicket(Ticket ticket) throws JsonException{
        User user = userService.getUserById(ticket.getUserId());
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        ticketService.updateTicket(ticket);
        return AjaxResult.success();
    }

    /**
     * @desc 删除优惠券
     * @param id
     * @return
     * @throws JsonException
     */
    @PostMapping("/ticket/delete")
    public AjaxResult deleteTicket(int id) throws JsonException {
        Ticket ticket = ticketService.getTicket(id);
        if (ticket == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        ticketService.deleteticket(id);
        return AjaxResult.success();
    }
}
